<template>
  <div class="list">
    <Breadcrumb name1="商品管理" name2="商品列表" />
    <el-card>
      <!-- 按钮搜索/添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" @clear="getGoofsList" clearable v-model="queryInfo.query">
            <el-button v-slot="append" icon="el-icon-search" @click="getGoofsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table区域 -->
      <el-row>
        <el-table :data="goodsList" border style="width: 100%">
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column prop="goodsName" label="药品名称"></el-table-column>
          <el-table-column prop="goodsPrice" label="价格(元)" width="110px"></el-table-column>
          <!-- <el-table-column
            prop="goods_number"
            width="120px"
            label="商品数量"
          ></el-table-column> -->
          <el-table-column prop="goodsWeight" label="药品规格" width="130px"></el-table-column>
          <el-table-column prop="goodsNumber" label="药品数量" width="130px"></el-table-column>
          <el-table-column width="220px" prop="addTime" label="添加药品时间">
          </el-table-column>

          <el-table-column label="操作" width="150px">
            <template v-slot="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click='gai(scope.row.id)'></el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[7, 11, 15, 19]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>



    <el-dialog
      title="编辑药品"
      :visible.sync="isclose"
      width="50%">
      <!-- 内容主体 -->
      <el-form :model="addForm" :rules="addFormRules" ref="ruleFormRef"
               label-width="70px">
        <el-form-item label="药品名称" prop="goodsName">
          <el-input class="input2" v-model="addForm.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="药品单价" prop="goodsPrice">
          <el-input class="input2" v-model="addForm.goodsPrice"></el-input>
        </el-form-item>
        <el-form-item label="药品规格" prop="goodsWeight">
          <el-input class="input2" v-model="addForm.goodsWeight"></el-input>
        </el-form-item>
<!--        <el-form-item label="适应症" prop="userId">-->
<!--          <el-input class="input2" v-model="addForm.userId"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="药品数量" prop="goodsNumber">
          <el-input class="input2" v-model="addForm.goodsNumber"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="isclose = false;">取 消</el-button>
    <el-button type="primary" @click="handleupdate">确 定</el-button>
  </span>
    </el-dialog>

<!--    新增-->
    <el-dialog
      title="添加药品"
      :visible.sync="dialogVisible"
      style='margin-left: 20px'
    >
      <!-- 内容主体 -->
      <el-form :model="addForm" :rules="addFormRules" ref="ruleFormRef"
               label-width="70px">

        <el-form-item label="药品名称" prop="goodsName">
          <el-input class="input2" v-model="addForm.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="药品单价" prop="goodsPrice">
          <el-input class="input2" v-model="addForm.goodsPrice"></el-input>
        </el-form-item>
        <el-form-item label="药品规格" prop="goodsWeight">
          <el-input class="input2" v-model="addForm.goodsWeight"></el-input>
        </el-form-item>
        <!--        <el-form-item label="适应症" prop="userId">-->
        <!--          <el-input class="input2" v-model="addForm.userId"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="药品数量" prop="goodsNumber">
          <el-input class="input2" v-model="addForm.goodsNumber"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleAdd">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from 'components/content/breadcrumb/Breadcrumb'
import axios from 'axios'
export default {
  name: 'List',
  components: {
    Breadcrumb
  },
  data() {
    return {
      dialogVisible:false,
      addFormRules: {
        id: [
          {required: true, message: 'id', trigger: 'blur'},
          {min: 1, max: 4, message: '药品编号在1-4位数之间', trigger: 'blur'}
        ],
        cgnName: [
          {required: true, message: '请输入药品名称', trigger: 'blur'},
          {min: 2, max: 23, message: '药品名称在1-23个字符之间', trigger: 'blur'}
        ],
        userId: [
          {required: true, message: '请输入药品数量', trigger: 'blur'},
          {min: 1, max: 5, message: '药品数量在1-99999之间', trigger: 'blur'}
        ],
        cgnTel: [
          {required: true, message: '请输入药品规格', trigger: 'blur'},
          {min: 1, max: 10, message: '请输入1-10个字符之间', trigger: 'blur'}
        ],
        cgnAddress: [
          {required: true, message: '请输入药品位置', trigger: 'blur'},
          {min: 6, max: 50, message: '字段长度在6-50个字符之间', trigger: 'blur'}
        ],

      },
      addForm: {
        goodsName: '',
        goodsPrice: '',
        goodsNumber: '',
        goodsWeight: '',
      },
      isclose:false,
      param:'',
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 7
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created() {
    this.getGoofsList()
  },
  methods: {
    handleAdd(){

      axios.post("/goods/", this.addForm).then((res) => {
        if (res.data.flg) {
          this.dialogVisible = false
          this.$message({
            message: '添加成功！！',
            type: 'success'
          });
        } else {
          this.$message.error('添加失败');
        }
      }).finally(() => {
        this.getGoofsList()
      })
    },




    // 更改商品
    gai(row){

      axios.get("/goods/"+row).then((res)=>{

        if(res.data.flg && res.data.data!=null){
          this.isclose=true
          this.addForm=res.data.data;
          console.log(this.addForm)
        }else {
          this.$message.error("数据同步失败，自动刷新")
        }
      }).finally(() => {
        this.getGoofsList()
      })
    },



forminit()
{
  this.addForm={}
},
    handleupdate(){
      axios.put("/goods/",this.addForm).then((res)=>{
        if(res.data.flg){
          this.isclose=false
          this.$message.success("数据编辑成功")

        }else {
          this.$message.error("数据同步失败")
        }
      })
        .finally(() => {
          this.forminit()
          this.getGoofsList()
        })
      this.isclose=false
    },

    // 根据分页获取列表数据
    async getGoofsList() {
      if(this.queryInfo.query!=null){
        this.param="?goodsName="+this.queryInfo.query;
      }
      // https://lianghj.top:8888/api/private/v1/goods
      const { data: res } = await this.$http.get('/goods/'+this.queryInfo.pagenum+'/'+this.queryInfo.pagesize+this.param )
      if (res.flg !== true) {
        return this.$message.error('获取商品列表失败!')
      }
      this.$message.success('获取商品列表成功!')
      this.goodsList = res.data.records
      this.total = res.data.total
      console.log(res.data)
    },
    // 显示的页数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoofsList()
    },
    // 页码的改变
    handleCurrentChange(newValue) {
      this.queryInfo.pagenum = newValue
      this.getGoofsList()
    },
    async removeById(id) {

      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('删除操作已取消!')
      }
      // https://lianghj.top:8888/api/private/v1/goods/
      const { data: res } = await this.$http.delete('/goods/' +id)
      if (res.flg !== true) {
        console.log(res)
        return this.$message.error('删除失败!')
      }
      this.$message.success('删除成功!')
      this.getGoofsList()
    },

    goAddpage() {
      // this.$router.push('goods/add')
      this.dialogVisible=true
      this.forminit()
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 15px 0 0;
}
.input2{
 width: 85%;
}
</style>
